home *** CD-ROM | disk | FTP | other *** search
/ SPACE 1 / SPACE - Library 1 - Volume 1.iso / utilitys / 47 / viscalc / curses.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-03-30  |  8.1 KB  |  381 lines

  1. /**********************************************************************
  2.  
  3.  *
  4.  
  5.  * Tiny pseudo "curses" package (runs on U__X, VMS, or MCH_AMIGA)
  6.  
  7.  *
  8.  
  9.  *    v1.0    870117    DBW - D. Wecker, initial hack
  10.  
  11.  *
  12.  
  13.  **********************************************************************/
  14.  
  15.  
  16.  
  17. #ifdef VMS
  18.  
  19. #include <stsdef.h>
  20.  
  21. #include <ssdef.h>
  22.  
  23. #include <descrip.h>
  24.  
  25. #include <iodef.h>
  26.  
  27. #include <ttdef.h>
  28.  
  29.  
  30.  
  31. #define NIBUF   128        /* Input buffer size */
  32.  
  33. #define NOBUF   1024        /* MM says bug buffers win! */
  34.  
  35. #define EFN     0        /* Event flag */
  36.  
  37.  
  38.  
  39. char obuf[NOBUF];        /* Output buffer */
  40.  
  41. int nobuf;            /* # of bytes in above */
  42.  
  43. char ibuf[NIBUF];        /* Input buffer */
  44.  
  45. int nibuf;            /* # of bytes in above */
  46.  
  47. int ibufi;            /* Read index */
  48.  
  49. int oldmode[2];            /* Old TTY mode bits */
  50.  
  51. int newmode[2];            /* New TTY mode bits */
  52.  
  53. short iochan;            /* TTY I/O channel */
  54.  
  55. struct dsc$descriptor  idsc;
  56.  
  57. struct dsc$descriptor  odsc;
  58.  
  59. char oname[40];
  60.  
  61. int iosb[2];
  62.  
  63. int term[2];
  64.  
  65. int status;
  66.  
  67. #endif
  68.  
  69.  
  70.  
  71. #ifdef MCH_AMIGA
  72.  
  73. extern    char        *Open();
  74.  
  75. extern    long        Read(),Write();
  76.  
  77. extern    void        Close();
  78.  
  79. #define NEW        1006L
  80.  
  81. #define AMG_MAXBUF    1024
  82.  
  83. static char        *terminal = 0L;
  84.  
  85. static char        scrn_tmp[AMG_MAXBUF+1];
  86.  
  87. static long        scrn_tmp_p = 0L;
  88.  
  89. #endif
  90.  
  91.  
  92.  
  93. #ifdef U__X
  94.  
  95. #include <sys/ioctl.h>
  96.  
  97. #include <sgtty.h>
  98.  
  99. #include <stdio.h>
  100.  
  101. struct sgttyb old_tty,new_tty;
  102.  
  103. #endif
  104.  
  105.  
  106.  
  107. #ifdef MCH_AMIGA
  108.  
  109. #define    COLS    79
  110.  
  111. #define ROWS    23
  112.  
  113. #else
  114.  
  115. #define    COLS    80
  116.  
  117. #define ROWS    24
  118.  
  119. #endif
  120.  
  121.  
  122.  
  123. #ifdef TOS
  124.  
  125. #include <osbind.h>
  126.  
  127. #define ST_MAXBUF 512
  128.  
  129. static char        scrn_tmp[ST_MAXBUF+1];
  130.  
  131. static long        scrn_tmp_p = 0L;
  132.  
  133. #endif
  134.  
  135.  
  136.  
  137. #define NORMAL    0x00
  138.  
  139. #define BOLD    0x80
  140.  
  141.  
  142.  
  143. char    nscrn[ROWS][COLS],
  144.  
  145.     cscrn[ROWS][COLS],
  146.  
  147.     row,
  148.  
  149.     col,
  150.  
  151.     mode;
  152.  
  153. char    str[256];
  154.  
  155.  
  156.  
  157. move(y,x)
  158.  
  159. int y,x;
  160.  
  161.     {
  162.  
  163.     row = y;
  164.  
  165.     col = x;
  166.  
  167.     }
  168.  
  169.  
  170.  
  171. clrtoeol() {
  172.  
  173.     int i;
  174.  
  175.  
  176.  
  177.     for (i = col; i < COLS; i++) nscrn[row][i] = ' ' | mode;
  178.  
  179.     }
  180.  
  181.  
  182.  
  183. printw(fmt,a1,a2,a3,a4,a5)
  184.  
  185. char    *fmt,*a1,*a2,*a3,*a4,*a5;
  186.  
  187.     {
  188.  
  189.     int i,j;
  190.  
  191.  
  192.  
  193.     sprintf(str,fmt,a1,a2,a3,a4,a5);
  194.  
  195.     j = 0;
  196.  
  197.     for (i = col; i < COLS && str[j] != '\000'; i++)
  198.  
  199.     nscrn[row][i] = str[j++] | mode;
  200.  
  201.     col = i;
  202.  
  203.     }
  204.  
  205.  
  206.  
  207. clrtobot() {
  208.  
  209.     int i,j;
  210.  
  211.  
  212.  
  213.     clrtoeol();
  214.  
  215.     for (i = row+1; i < ROWS; i++)
  216.  
  217.     for (j = 0; j < COLS; j++)
  218.  
  219.         nscrn[i][j] = ' ' | mode;
  220.  
  221.     }
  222.  
  223.  
  224.  
  225. standout() {
  226.  
  227.     mode = BOLD;
  228.  
  229.     }
  230.  
  231.  
  232.  
  233. standend() {
  234.  
  235.     mode = NORMAL;
  236.  
  237.     }
  238.  
  239.  
  240.  
  241. addstr(s)
  242.  
  243. char    *s;
  244.  
  245.     {
  246.  
  247.     printw("%s",s);
  248.  
  249.     }
  250.  
  251.  
  252.  
  253. initscr() {
  254.  
  255.     int        i,j;
  256.  
  257.  
  258.  
  259. #ifdef MCH_AMIGA
  260.  
  261.     terminal = Open("RAW:1/1/639/199/DBW_VC (v1.0 870117)",(long)NEW);
  262.  
  263. #endif
  264.  
  265. #ifdef VMS
  266.  
  267.     odsc.dsc$a_pointer = "TT";
  268.  
  269.     odsc.dsc$w_length = strlen(odsc.dsc$a_pointer);
  270.  
  271.     odsc.dsc$b_dtype = DSC$K_DTYPE_T;
  272.  
  273.     odsc.dsc$b_class = DSC$K_CLASS_S;
  274.  
  275.     idsc.dsc$b_dtype = DSC$K_DTYPE_T;
  276.  
  277.     idsc.dsc$b_class = DSC$K_CLASS_S;
  278.  
  279.     do {
  280.  
  281.     idsc.dsc$a_pointer = odsc.dsc$a_pointer;
  282.  
  283.     idsc.dsc$w_length = odsc.dsc$w_length;
  284.  
  285.     odsc.dsc$a_pointer = &oname[0];
  286.  
  287.     odsc.dsc$w_length = sizeof(oname);
  288.  
  289.     status = LIB$SYS_TRNLOG(&idsc, &odsc.dsc$w_length, &odsc);
  290.  
  291.     if (status!=SS$_NORMAL && status!=SS$_NOTRAN) exit(status);
  292.  
  293.     if (oname[0] == 0x1B) {
  294.  
  295.         odsc.dsc$a_pointer += 4;
  296.  
  297.         odsc.dsc$w_length -= 4;
  298.  
  299.         }
  300.  
  301.     }
  302.  
  303.     while (status == SS$_NORMAL);
  304.  
  305.     status = SYS$ASSIGN(&odsc, &iochan, 0, 0);
  306.  
  307.     if (status != SS$_NORMAL) exit(status);
  308.  
  309.     status = SYS$QIOW(EFN, iochan, IO$_SENSEMODE, iosb, 0, 0,
  310.  
  311.         oldmode, sizeof(oldmode), 0, 0, 0, 0);
  312.  
  313.     if (status!=SS$_NORMAL || (iosb[0]&0xFFFF)!=SS$_NORMAL) exit(status);
  314.  
  315.     newmode[0] = oldmode[0];
  316.  
  317.     newmode[1] = oldmode[1] | TT$M_PASSALL | TT$M_NOECHO;
  318.  
  319.     status = SYS$QIOW(EFN, iochan, IO$_SETMODE, iosb, 0, 0,
  320.  
  321.         newmode, sizeof(newmode), 0, 0, 0, 0);
  322.  
  323.     if (status!=SS$_NORMAL || (iosb[0]&0xFFFF)!=SS$_NORMAL) exit(status);
  324.  
  325. #endif
  326.  
  327.  
  328.  
  329. #ifdef U__X
  330.  
  331.     ioctl(0,TIOCGETP,&old_tty);
  332.  
  333.     ioctl(0,TIOCGETP,&new_tty);
  334.  
  335.     new_tty.sg_flags |= RAW;
  336.  
  337.     new_tty.sg_flags &= ~ECHO;
  338.  
  339.     ioctl(0,TIOCSETP,&new_tty);
  340.  
  341. #endif
  342.  
  343.  
  344.  
  345.     row        = 0;
  346.  
  347.     col        = 0;
  348.  
  349.     mode    = NORMAL;
  350.  
  351.     for (i = 0; i < ROWS; i++)
  352.  
  353.     for (j = 0; j < COLS; j++)
  354.  
  355.         nscrn[i][j] = cscrn[i][j] = ' ';
  356.  
  357.     ttputs("\033E");
  358.  
  359.     }
  360.  
  361.  
  362.  
  363. clear() {
  364.  
  365.     row = 0;
  366.  
  367.     col = 0;
  368.  
  369.     clrtobot();
  370.  
  371.     }
  372.  
  373.  
  374.  
  375. endwin() {
  376.  
  377.     move(ROWS-1,0);
  378.  
  379.     refresh();
  380.  
  381.  
  382.  
  383. #ifdef MCH_AMIGA
  384.  
  385.     amg_flush();
  386.  
  387.     Close(terminal);
  388.  
  389. #endif
  390.  
  391.  
  392.  
  393. #ifdef VMS
  394.  
  395.     status = SYS$QIOW(EFN, iochan, IO$_SETMODE, iosb, 0, 0,
  396.  
  397.         oldmode, sizeof(oldmode), 0, 0, 0, 0);
  398.  
  399.     if (status!=SS$_NORMAL || (iosb[0]&0xFFFF)!=SS$_NORMAL) exit(status);
  400.  
  401.     status = SYS$DASSGN(iochan);
  402.  
  403.     if (status != SS$_NORMAL) exit(status);
  404.  
  405. #endif
  406.  
  407.  
  408.  
  409. #ifdef U__X
  410.  
  411.     ioctl(0,TIOCSETP,&old_tty);
  412.  
  413. #endif
  414.  
  415.  
  416.  
  417.     }
  418.  
  419.  
  420.  
  421. char inch() {
  422.  
  423.     return(nscrn[row][col] & 0x7F);
  424.  
  425.     }
  426.  
  427.  
  428.  
  429. touchwin() {
  430.  
  431.     int i,j;
  432.  
  433.  
  434.  
  435.     for (i=0; i<ROWS; i++)
  436.  
  437.     for (j=0; j<COLS; j++)
  438.  
  439.         cscrn[i][j] = ' ';
  440.  
  441.     ttputs("\033E");
  442.  
  443.     }
  444.  
  445.  
  446.  
  447. refresh() {
  448.  
  449.     int    i,j,mode;
  450.  
  451.  
  452.  
  453.     mode = NORMAL;
  454.  
  455.     for (i=0; i < ROWS; i++) {
  456.  
  457.     for (j = 0; j < COLS; j++) {
  458.  
  459.         if (nscrn[i][j] != cscrn[i][j]) {
  460.  
  461.         sprintf(str,"\033Y%c%c",i+32,j+32);
  462.  
  463.         ttputs(str);
  464.  
  465.         while (nscrn[i][j] != cscrn[i][j]) {
  466.  
  467.             if (mode == NORMAL && (nscrn[i][j] & BOLD) == BOLD) {
  468.  
  469.             ttputs("\033p");
  470.  
  471.             mode = BOLD;
  472.  
  473.             }
  474.  
  475.             else if (mode == BOLD && (nscrn[i][j] & BOLD) == NORMAL) {
  476.  
  477.             ttputs("\033q");
  478.  
  479.             mode = NORMAL;
  480.  
  481.             }
  482.  
  483.             cscrn[i][j] = nscrn[i][j];
  484.  
  485.             ttputc(nscrn[i][j] & 0x7F);
  486.  
  487.             j++;
  488.  
  489.             }
  490.  
  491.         }
  492.  
  493.         }
  494.  
  495.     }
  496.  
  497.     sprintf(str,"\033Y%c%c",row+32,col+32);
  498.  
  499.     ttputs(str);
  500.  
  501.     if (mode) ttputs("\033q");
  502.  
  503.     ttflush();
  504.  
  505.     }
  506.  
  507.  
  508.  
  509. ttgetc() {
  510.  
  511. #ifdef MCH_AMIGA
  512.  
  513.     unsigned char ch[2];
  514.  
  515.  
  516.  
  517.     Read(terminal, ch, 1L);
  518.  
  519.     return (ch[0] & 0xFF);
  520.  
  521. #endif
  522.  
  523.  
  524.  
  525. #ifdef VMS
  526.  
  527.     while (ibufi >= nibuf) {
  528.  
  529.     ibufi = 0;
  530.  
  531.     term[0] = 0;
  532.  
  533.     term[1] = 0;
  534.  
  535.     status = SYS$QIOW(EFN, iochan, IO$_READLBLK|IO$M_TIMED,
  536.  
  537.     iosb, 0, 0, ibuf, NIBUF, 0, term, 0, 0);
  538.  
  539.     if (status != SS$_NORMAL) exit(status);
  540.  
  541.     status = iosb[0] & 0xFFFF;
  542.  
  543.     if (status!=SS$_NORMAL && status!=SS$_TIMEOUT) exit(status);
  544.  
  545.     nibuf = (iosb[0]>>16) + (iosb[1]>>16);
  546.  
  547.     if (nibuf == 0) {
  548.  
  549.         status = SYS$QIOW(EFN, iochan, IO$_READLBLK,
  550.  
  551.         iosb, 0, 0, ibuf, 1, 0, term, 0, 0);
  552.  
  553.         if (status != SS$_NORMAL || (status = (iosb[0]&0xFFFF)) != SS$_NORMAL)
  554.  
  555.         exit(status);
  556.  
  557.         nibuf = (iosb[0]>>16) + (iosb[1]>>16);
  558.  
  559.         }
  560.  
  561.     }
  562.  
  563.     return (ibuf[ibufi++] & 0x7F);
  564.  
  565. #endif
  566.  
  567.  
  568.  
  569. #ifdef U__X
  570.  
  571.     return(getchar() & 0x7F);
  572.  
  573. #endif
  574.  
  575.  
  576.  
  577. #ifdef TOS
  578.  
  579.     char c, hi, lo;
  580.  
  581.     long keycode=Crawcin();
  582.  
  583.     lo=keycode&0xff;
  584.  
  585.     hi=(keycode>>16)&0xff;
  586.  
  587.     switch (hi) {
  588.  
  589.         case 0x62 : {c='?'; break;}
  590.  
  591.         case 0x48 : {c='\020'; break;}
  592.  
  593.         case 0x4b : {c='\002'; break;}
  594.  
  595.         case 0x4d : {c='\006'; break;}
  596.  
  597.         case 0x50 : {c='\016'; break;}
  598.  
  599.         default : {c=lo;}
  600.  
  601.     }
  602.  
  603.     return (c);
  604.  
  605.  
  606.  
  607. #endif
  608.  
  609.     }
  610.  
  611.  
  612.  
  613. ttputc(c)
  614.  
  615. #ifdef MCH_AMIGA
  616.  
  617. char c;
  618.  
  619. #endif
  620.  
  621.     {
  622.  
  623. #ifdef MCH_AMIGA
  624.  
  625.     scrn_tmp[scrn_tmp_p++] = c;
  626.  
  627.     if(scrn_tmp_p>=AMG_MAXBUF) amg_flush();
  628.  
  629. #endif
  630.  
  631.  
  632.  
  633. #ifdef TOS
  634.  
  635.     scrn_tmp[scrn_tmp_p++] = c;
  636.  
  637.     if(scrn_tmp_p>=ST_MAXBUF) st_flush();
  638.  
  639. #endif
  640.  
  641.  
  642.  
  643. #ifdef VMS
  644.  
  645.     if (nobuf >= NOBUF) ttflush();
  646.  
  647.     obuf[nobuf++] = c;
  648.  
  649. #endif
  650.  
  651.  
  652.  
  653. #ifdef U__X
  654.  
  655.     fputc(c, stdout);
  656.  
  657. #endif
  658.  
  659.     }
  660.  
  661.  
  662.  
  663. #ifdef MCH_AMIGA
  664.  
  665. amg_flush()
  666.  
  667.     {
  668.  
  669.     if(scrn_tmp_p) Write(terminal,scrn_tmp,(long)scrn_tmp_p);
  670.  
  671.     scrn_tmp_p = 0;
  672.  
  673.     }
  674.  
  675. #endif
  676.  
  677.  
  678.  
  679. #ifdef TOS
  680.  
  681. st_flush()
  682.  
  683.     {
  684.  
  685.     scrn_tmp[scrn_tmp_p]=0;
  686.  
  687.     if(scrn_tmp_p) cputs(scrn_tmp);
  688.  
  689.     scrn_tmp_p = 0;
  690.  
  691.     }
  692.  
  693. #endif
  694.  
  695.  
  696.  
  697. ttputs(s)
  698.  
  699. char    *s;
  700.  
  701.     {
  702.  
  703.     while (*s) ttputc(*s++);
  704.  
  705.     }
  706.  
  707.  
  708.  
  709. ttflush()
  710.  
  711.     {
  712.  
  713. #ifdef MCH_AMIGA
  714.  
  715.     amg_flush();
  716.  
  717. #endif
  718.  
  719.  
  720.  
  721. #ifdef TOS
  722.  
  723.     st_flush();
  724.  
  725. #endif
  726.  
  727.  
  728.  
  729. #ifdef VMS
  730.  
  731.     status = SS$_NORMAL;
  732.  
  733.     if (nobuf != 0) {
  734.  
  735.     status = SYS$QIOW(EFN, iochan, IO$_WRITELBLK|IO$M_NOFORMAT,
  736.  
  737.     iosb, 0, 0, obuf, nobuf, 0, 0, 0, 0);
  738.  
  739.     if (status == SS$_NORMAL) status = iosb[0] & 0xFFFF;
  740.  
  741.     nobuf = 0;
  742.  
  743.     }
  744.  
  745.     return (status);
  746.  
  747. #endif
  748.  
  749.  
  750.  
  751. #ifdef U__X
  752.  
  753.     fflush(stdout);
  754.  
  755. #endif
  756.  
  757.     }
  758.  
  759.  
  760.  
  761.